﻿// 有关“搜索联系人”模板的简介，请参阅以下文档:
// http://go.microsoft.com/fwlink/?LinkId=232512

// TODO: 将以下脚本标记添加到起始页的标头中以
// 订阅搜索联系人事件。
//  
// <script src="/searchResults.js"></script>

(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var appModel = Windows.ApplicationModel;
    var appViewState = Windows.UI.ViewManagement.ApplicationViewState;
    var nav = WinJS.Navigation;
    var ui = WinJS.UI;
    var utils = WinJS.Utilities;
    var searchPageURI = "/pages/search/searchResults.html";
    var requestUrl = {
        video: 'http://app.hbook.us/flashinterface/GetMovieByPage.ashx?page=1&pagesize=1000&topicid=5&title=',
        news: 'http://app.hbook.us/flashinterface/GetNewsByPage.ashx?page=1&pagesize=1000&topicid=5&title=',
        picture: 'http://app.hbook.us/flashinterface/GetPicturesByPage.ashx?page=1&pagesize=1000&topicid=5&title='
    }


    ui.Pages.define(searchPageURI, {
        ready: function (element, options) {
            var queryText = encodeURIComponent(options.queryText);
            $('.search-query-txt').html(options.queryText);
            var Data = page.fun.getVideoDataList(requestUrl.video + queryText);
            var temp = setInterval(function () {
                if (page.fun.noData) {
                    clearInterval(temp);
                        $('.listPage-wrap h2').show();
                        $('.loading').hide();
                        page.fun.noData = false;

                }
                else if (Data.length > 1) {
                    clearInterval(temp);
                    page.fun.bindDate(
                        {
                            dataType: "video",
                            element: element,
                            dataList: Data,
                            listviewID: "#listPage-listView",
                            listTemplate: "#listPageTemplateWrap",
                            LayoutType: "GridLayout",
                            loadMorePages: true
                        }
                    );
                }

            },10)


            var searchObject = $('#search-category a');
            searchObject.eq(0).addClass('active');
            searchObject.click(function (eventInfo) {
                if ($(this).hasClass('active')) {
                    return;
                }
                $('.loading').show();
                $('.listPage-wrap h2').hide();
                searchObject.removeClass('active');
                $(this).addClass('active');
                switch ($(this).attr('id')) {
                    case 'videoResults-btn':
                        var Data = page.fun.getVideoDataList(requestUrl.video + queryText);
                        var temp = setInterval(function () {
                            if (page.fun.noData) {
                                clearInterval(temp);
                                $('.listPage-wrap h2').show();
                                $('.loading').hide();
                                page.fun.noData = false;

                            }
                            else if (Data.length > 1) {
                                clearInterval(temp);
                                page.fun.bindDate(
                                    {
                                        dataType: "video",
                                        element: element,
                                        dataList: Data,
                                        listviewID: "#listPage-listView",
                                        listTemplate: "#listPageTemplateWrap",
                                        LayoutType: "GridLayout",
                                        loadMorePages: true
                                    }
                                );
                            }
                        }, 10)

                        break;
                    case 'newsResults-btn':
                        var Data = page.fun.getNewsDataList(requestUrl.news + queryText);
                        var temp = setInterval(function () {
                            if (page.fun.noData) {
                                clearInterval(temp);
                                $('.listPage-wrap h2').html('无相关新闻结果').show();
                                $('.loading').hide();
                                page.fun.noData = false;

                            }
                            else if (Data.length > 1) {
                                clearInterval(temp);
                                page.fun.bindDate(
                                    {
                                        dataType: "news",
                                        element: element,
                                        dataList: Data,
                                        listviewID: "#listPage-listView",
                                        listTemplate: "#listPageTemplateWrap",
                                        LayoutType: "GridLayout",
                                        loadMorePages: true
                                    }
                                );
                            }
                        }, 10)
                        break;
                    case 'pictureResults-btn':
                        var Data = page.fun.getPictureDataList(requestUrl.picture + queryText);
                        var temp = setInterval(function () {
                            if (page.fun.noData) {
                                clearInterval(temp);
                                $('.listPage-wrap h2').html('无相关图片结果').show();
                                $('.loading').hide();
                                page.fun.noData = false;

                            }
                            else if (Data.length > 1) {
                                clearInterval(temp);
                                page.fun.bindDate(
                                    {
                                        dataType: "picture",
                                        element: element,
                                        dataList: Data,
                                        listviewID: "#listPage-listView",
                                        listTemplate: "#listPageTemplateWrap",
                                        LayoutType: "GridLayout",
                                        loadMorePages: true
                                    }
                                );
                            }
                        }, 10)
                        break;
                }
                eventInfo.preventDefault();
            });
        },

        updateLayout: function (element, viewState, lastViewState) {

        }
    });

    WinJS.Application.addEventListener("activated", function (args) {
        if (args.detail.kind === appModel.Activation.ActivationKind.search) {
            args.setPromise(ui.processAll().then(function () {
                if (!nav.location) {
                    nav.history.current = { location: Application.navigator.home, initialState: {} };
                }

                return nav.navigate(searchPageURI, { queryText: args.detail.queryText });
            }));
        }
    });

})();
